import { toastShort } from ".";
import { DocOptionHistory } from "./types";

class UndoRedoHistory {
  private history: DocOptionHistory[];
  private currentIndex: number;

  constructor() {
    this.history = [];
    this.currentIndex = -1;
  }

  addAction(state: DocOptionHistory): void {
    // if(this.history.length > 3){
    //   this.history = this.history.splice(this.history.length - 3, 3);
    // }
    console.log("测试----->addAction", state)
    // this.history.length = this.currentIndex + 1;
    this.history.push(state);
    // this.currentIndex + 1
    this.currentIndex++;
  }

  undo(): void {
    if (this.currentIndex > 0) {
      this.currentIndex--;
    }
  }

  redo(): void {
    if (this.currentIndex < this.history.length - 1) {
      this.currentIndex++;
    }
  }

  getCurrentState(): DocOptionHistory {
    console.log("this.history", this.history, this.currentIndex);
    if(this.currentIndex == -1 || this.currentIndex > this.history.length){
      toastShort('warning', '暂无操作记录');
      return null;
    }
    return this.history[this.currentIndex];
  }

  getCurrentIndex() {
    return this.currentIndex;
  }

  getCurrentLend() {
    return this.history.length;
  }
}


export default UndoRedoHistory;